using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Krilib.Data;

namespace Krile.Forms.Dialogs.Editor.SearchData
{
    public partial class SearchDataClusterEditor : Form
    {
        /// <summary>
        /// Get/set search data cluster
        /// </summary>
        public SearchDataCluster SearchDataCluster
        {
            get
            {
                SearchDataCluster retClustor = new SearchDataCluster(SearchDataArray);
                retClustor.UseOrMatch = UseOrMatch;
                return retClustor;
            }
            set
            {
                if (value == null)
                    return;
                SearchDataArray = value.SearchDatas;
                UseOrMatch = value.UseOrMatch;
            }
        }

        /// <summary>
        /// Search data array
        /// </summary>
        public SearchDataBase[] SearchDataArray
        {
            get
            {
                List<SearchDataBase> retlist = new List<SearchDataBase>();
                foreach (object item in searchDataList.Items)
                {
                    SearchDataBase sdb = item as SearchDataBase;
                    if (sdb != null)
                        retlist.Add(sdb);
                }
                return retlist.ToArray();
            }
            set
            {
                searchDataList.Items.Clear();
                if (value == null) return;
                foreach (var val in value)
                {
                    if (val != null)
                        searchDataList.Items.Add(val);
                }
            }
        }

        public bool UseOrMatch
        {
            get { return useOrMatch.Checked; }
            set { useOrMatch.Checked = value; }
        }

        public SearchDataClusterEditor()
        {
            InitializeComponent();
        }

        public SearchDataClusterEditor(SearchDataCluster cluster) : this()
        {
            SearchDataCluster = cluster;
        }

        private void addNewItem_Click(object sender, EventArgs e)
        {
            AddNewSelectStrip.Show(addNewItem, new Point(0, addNewItem.Height));
        }

        private void addNewCluser_Click(object sender, EventArgs e)
        {
            using (var sde = new SearchDataClusterEditor())
            {
                if (sde.ShowDialog() == DialogResult.OK)
                {
                    searchDataList.Items.Add(sde.SearchDataCluster);
                }
            }
        }

        private void addNewSearchData_Click(object sender, EventArgs e)
        {
            using (var sde = new SearchDataEditor())
            {
                if (sde.ShowDialog() == DialogResult.OK)
                {
                    searchDataList.Items.Add(sde.SelectedData);
                }
            }
        }

        private void searchDataList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (searchDataList.SelectedIndex == -1) return;
            if (searchDataList.SelectedItem is SearchDataCluster)
            {
                using (var sde = new SearchDataClusterEditor())
                {
                    sde.SearchDataCluster = (SearchDataCluster)searchDataList.SelectedItem;
                    if (sde.ShowDialog() == DialogResult.OK)
                    {
                        searchDataList.Items[searchDataList.SelectedIndex] = sde.SearchDataCluster;
                    }
                }
            }
            else
            {
                //Search data
                using (var sde = new SearchDataEditor())
                {
                    sde.SelectedData = (Krilib.Data.SearchData)searchDataList.SelectedItem;
                    if (sde.ShowDialog() == DialogResult.OK)
                    {
                        searchDataList.Items[searchDataList.SelectedIndex] = sde.SelectedData;
                    }
                }
            }
        }

        private void searchDataList_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (searchDataList.SelectedIndex == -1) return;
                searchDataList.Items.RemoveAt(searchDataList.SelectedIndex);
            }
        }
    }
}